//
//  HJJPlayerViewController.m
//  Pianke
//
//  Created by 黄俊杰 on 15/12/27.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "HJJPlayerViewController.h"
#import "HJJPlayerImageView.h"
#import "HJJRadioRootModel.h"
#import "HJJPlayerListView.h"
#import "HJJPlayerWebView.h"
#import "Radio.h"
#import "CoreData.h"

@interface HJJPlayerViewController ()<UIScrollViewDelegate, STKAudioPlayerDelegate, PlayerListViewDelegate>

@property (nonatomic, strong)UIScrollView *myScroll;

@property (nonatomic, strong)UILabel *titleLabel;

@property (nonatomic, strong)UIImageView *bgImage;

@property (nonatomic, strong)UILabel *startTime;

@property (nonatomic, strong)UILabel *totalTime;

@property (nonatomic, strong)HJJPlayerImageView *playerImageView;

@property (nonatomic, strong)HJJPlayerListView *playerListView;

@property (nonatomic, strong)HJJPlayerWebView *playerWebView;

@property (nonatomic, strong)UIButton *typeBtn;

@property (nonatomic, assign)NSInteger typeKey;

@property (nonatomic, strong)UIPageControl *pageC;

@property (nonatomic, strong)UISlider *mySlider;

@property (nonatomic, strong)NSMutableArray *authorArray;

@property (nonatomic, assign)NSInteger tempRow;

@property (nonatomic, strong)UIView *animationView;

@property (nonatomic, strong)UILabel *animationLabel;

@property (nonatomic, assign)BOOL btnState;

@property (nonatomic, assign)BOOL likeState;

@property (nonatomic, strong)UIButton *likeBtn;

@property (nonatomic, retain)MBProgressHUD *hud;

@end

@implementation HJJPlayerViewController

+(instancetype)sharePlayerViewController{
    static HJJPlayerViewController *shareMusicPlayerVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareMusicPlayerVC = [[HJJPlayerViewController alloc]init];
    });
    return shareMusicPlayerVC;
}

-(void)viewDidAppear:(BOOL)animated{

    [self.startBtn setImage:[UIImage imageNamed:@"pause2.png"] forState:UIControlStateNormal];
    self.btnJudge = 1;
    self.smallPlayerKey = 0;
    [self giveValue];
    //判断btn状态(外来页面)
    if (self.outKey == 1) {
        [self startJudge];
    }
    
}

-(void)likeStateJudge{

    HJJRadioRootModel *model = [self.listArray objectAtIndex:self.indexRow];
    NSMutableArray *array = [CoreData selectData:1];
    if ([array containsObject:model.musicUrl]) {
        [self.likeBtn setImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];
        self.likeState = 1;
    }else{
    
        [self.likeBtn setImage:[UIImage imageNamed:@"disLike.png"] forState:UIControlStateNormal];
        self.likeState = 0;
    }
}

-(void)startJudge{

    if (self.player.state == STKAudioPlayerStatePlaying) {
        
        
        [self.startBtn setImage:[UIImage imageNamed:@"pause2.png"] forState:UIControlStateNormal];
        self.btnState = 1;
    }else{
    
        [self.startBtn setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
        self.btnState = 0;
    }
}

//小播放器btn触发方法
-(void)sendSmallState:(NSString *)State{

    if ([State isEqualToString:@"play"]) {
        [self.player resume];
        [self startAnimation];
    }else{
    
        [self.player pause];
        [self stopAnimation];
    }
    [self startJudge];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.outKey = 0;
    
    self.bgImage = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.bgImage.image = [UIImage imageNamed:@"musicBG.png"];
    self.bgImage.userInteractionEnabled = 1;
    [self.view addSubview:self.bgImage];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = BIGFONT;
    self.titleLabel.textColor = WHITECOLOR;
    self.titleLabel.textAlignment = 1;
    [self.bgImage addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(35* EACH_HEIGHT);
        make.left.mas_equalTo(self.view.mas_centerX).offset(-130 * EACH_WIDTH);
        make.right.mas_equalTo(self.view.mas_centerX).offset(130 * EACH_WIDTH);
        make.bottom.mas_equalTo(self.titleLabel.mas_top).offset(25* EACH_HEIGHT);
    }];

    
    self.myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40 * EACH_HEIGHT, 375 * EACH_WIDTH, 477 * EACH_HEIGHT)];
    self.myScroll.bounces = NO;
    self.myScroll.delegate = self;
    self.myScroll.showsHorizontalScrollIndicator = NO;
    self.myScroll.alwaysBounceHorizontal = 1;
    self.myScroll.pagingEnabled = 1;
    [self.myScroll setContentOffset:CGPointMake(375 * EACH_WIDTH, 0)];
    self.myScroll.contentSize = CGSizeMake(375 * EACH_WIDTH * 3, 300 * EACH_HEIGHT);
    [self.bgImage addSubview:self.myScroll];
    
    //pageController
    self.pageC = [[UIPageControl alloc] init];
    self.pageC.currentPageIndicatorTintColor = [UIColor colorWithRed:0.15 green:0.76 blue:0.48 alpha:1];
    self.pageC.pageIndicatorTintColor = WHITECOLOR;
    [self.bgImage addSubview:self.pageC];
    self.pageC.numberOfPages = 3;
    [self.pageC mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.myScroll.mas_bottom).offset(2* EACH_HEIGHT);
        make.bottom.mas_equalTo(self.pageC.mas_top).offset(15* EACH_HEIGHT);
        make.left.equalTo(self.view.mas_centerX).offset(-50 * EACH_WIDTH);
        make.right.equalTo(self.view.mas_centerX).offset(50 * EACH_WIDTH);
    }];
    
    //三个btn
    self.lastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.lastBtn setImage:[UIImage imageNamed:@"last.png"] forState:UIControlStateNormal];
    self.lastBtn.layer.cornerRadius = 20;
    self.lastBtn.layer.masksToBounds = 1;
    [self.lastBtn addTarget:self action:@selector(lastAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImage addSubview:self.lastBtn];
    [self.lastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(80 * EACH_WIDTH);
        make.top.mas_equalTo(self.myScroll.mas_bottom).offset(60 * EACH_HEIGHT);
        make.size.mas_equalTo(CGSizeMake(50* EACH_HEIGHT, 50* EACH_HEIGHT));
    }];
    
    self.nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.nextBtn setImage:[UIImage imageNamed:@"next2.png"] forState:UIControlStateNormal];
    self.nextBtn.layer.cornerRadius = 20;
    self.nextBtn.layer.masksToBounds = 1;
    [self.nextBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImage addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-80 * EACH_WIDTH);
        make.top.mas_equalTo(self.myScroll.mas_bottom).offset(60* EACH_HEIGHT);
        make.size.mas_equalTo(CGSizeMake(50* EACH_HEIGHT, 50* EACH_HEIGHT));
    }];
    
    
    self.startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.startBtn.layer.cornerRadius = 30 * EACH_HEIGHT;
    self.startBtn.layer.masksToBounds = 1;
    [self.startBtn setImage:[UIImage imageNamed:@"pause2.png"] forState:UIControlStateNormal];
    [self.startBtn addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImage addSubview:self.startBtn];
    [self.startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.myScroll.mas_bottom).offset(55 * EACH_HEIGHT);
        make.left.equalTo(self.view.mas_centerX).offset(-30 * EACH_WIDTH);
        make.right.equalTo(self.view.mas_centerX).offset(30 * EACH_WIDTH);
        make.bottom.mas_equalTo(-35* EACH_HEIGHT);
    }];
    
    //播放器对象
    self.player = [[STKAudioPlayer alloc]initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = YES, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }];
    self.player.delegate = self;
    
    //定时器
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    
    self.mySlider = [[UISlider alloc] init];
    //设置最小(最大)进度颜色,
    self.mySlider.minimumTrackTintColor = [UIColor colorWithRed:0.15 green:0.76 blue:0.48 alpha:1];
    self.mySlider.maximumTrackTintColor = BLUECOLOR;
    [self.bgImage addSubview:self.mySlider];
    [self.mySlider addTarget:self action:@selector(sliderAction) forControlEvents:UIControlEventValueChanged];
    [self.mySlider mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.myScroll.mas_bottom).offset(30* EACH_HEIGHT);
        make.left.mas_equalTo(60 * EACH_WIDTH);
        make.right.mas_equalTo(-60 * EACH_WIDTH);
        make.bottom.mas_equalTo(self.mySlider.mas_top).offset(15* EACH_HEIGHT);
    }];
    //设置滑轮背景图片
    [self.mySlider setThumbImage:[UIImage imageNamed:@"slider.png"] forState:UIControlStateNormal];
    
    self.startTime = [[UILabel alloc] init];
    self.startTime.font = MIDDLEFONT;
    self.startTime.textColor = GRAYCOLOR;
    self.startTime.textAlignment = 1;
    [self.bgImage addSubview:self.startTime];
    [self.startTime mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mySlider.mas_left).offset(-5 * EACH_WIDTH);
        make.top.bottom.mas_equalTo(self.mySlider);
    }];
    
    self.totalTime = [[UILabel alloc] init];
    self.totalTime.font = MIDDLEFONT;
    self.totalTime.textColor = GRAYCOLOR;
    self.totalTime.textAlignment = 1;
    [self.bgImage addSubview:self.totalTime];
    [self.totalTime mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.mySlider.mas_right).offset(5 * EACH_WIDTH);
        make.top.bottom.mas_equalTo(self.mySlider);
    }];
    
    //typeBtn
    self.typeKey = 0;
    self.typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.typeBtn setImage:[UIImage imageNamed:@"xunhuan.png"] forState:UIControlStateNormal];
    [self.typeBtn addTarget:self action:@selector(typeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImage addSubview:self.typeBtn];
    [self.typeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(20 * EACH_WIDTH);
        make.top.mas_equalTo(self.myScroll.mas_bottom).offset(60* EACH_HEIGHT);
        make.size.mas_equalTo(CGSizeMake(50* EACH_HEIGHT, 50* EACH_HEIGHT));
    }];
    
    //返回Btn
    UIButton *backBtn = [[UIButton alloc] init];
    [self.bgImage addSubview:backBtn];
    [backBtn setImage:[UIImage imageNamed:@"backBtn.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(25 * EACH_WIDTH);
        make.top.mas_equalTo(35* EACH_HEIGHT);
        make.size.mas_equalTo(CGSizeMake(30* EACH_HEIGHT, 30* EACH_HEIGHT));
    }];
    
    //收藏btn
    self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.likeBtn setImage:[UIImage imageNamed:@"disLike.png"] forState:UIControlStateNormal];
    self.likeBtn.layer.cornerRadius = 20;
    self.likeBtn.layer.masksToBounds = 1;
    [self.likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImage addSubview:self.likeBtn];
    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-15 * EACH_WIDTH);
        make.top.mas_equalTo(25* EACH_HEIGHT);
        make.size.mas_equalTo(CGSizeMake(50* EACH_HEIGHT, 50* EACH_HEIGHT));
    }];
    
    //playerImageView
    self.playerImageView = [[HJJPlayerImageView alloc] initWithFrame:CGRectMake(375 * EACH_WIDTH, 40 * EACH_HEIGHT, 375 * EACH_WIDTH, 477 * EACH_HEIGHT)];
    [self.myScroll addSubview:self.playerImageView];
    
    //playerListView
    self.playerListView = [[HJJPlayerListView alloc] initWithFrame:CGRectMake(0, 40 * EACH_HEIGHT, 375 * EACH_WIDTH, 477 * EACH_HEIGHT)];
    self.playerListView.playerListViewDele = self;
    [self.myScroll addSubview:self.playerListView];
    
    //webView
    self.playerWebView = [[HJJPlayerWebView alloc] initWithFrame:CGRectMake(375 * EACH_WIDTH * 2, 40 * EACH_HEIGHT, 375 * EACH_WIDTH, 477 * EACH_HEIGHT)];
    [self.myScroll addSubview:self.playerWebView];
    
    //动画
    self.animationView = [[UIView alloc] initWithFrame:CGRectMake(0, -60 * EACH_HEIGHT, 375 * EACH_WIDTH, 60 * EACH_HEIGHT)];
    self.animationView.backgroundColor = [UIColor clearColor];
    [self.bgImage addSubview:self.animationView];
    
    self.animationLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 * EACH_WIDTH, 38 * EACH_HEIGHT, 275 * EACH_WIDTH, 18 * EACH_HEIGHT)];
    self.animationLabel.textColor = WHITECOLOR;
    self.animationLabel.textAlignment = 1;
    self.animationLabel.font = MIDDLEFONT;
    [self.animationView addSubview:self.animationLabel];
    
    //设置当前page
    self.pageC.currentPage = 1;
    
}

-(void)giveValue{
  
    HJJRadioRootModel *model = [self.listArray objectAtIndex:self.indexRow];
    
    NSLog(@"model class %@", [model class]);
    if (self.outKey == 0) {
        //播放
        NSString *url = model.musicUrl;
        [self.player playURL:[NSURL URLWithString:url]];
    }
    
    //封面
    NSURL *picUrl = [NSURL URLWithString:model.coverimg];
    [self.playerImageView.PicImage sd_setImageWithURL:picUrl placeholderImage:nil];
    
    //标题
    self.titleLabel.text = model.title;
    self.playerImageView.nameLabel.text = [NSString stringWithFormat:@"- %@ -",model.uname];
    
    //传值给tableView
    [self.playerListView reciveData:self.listArray indexRow:self.indexRow];
    
    //传值给webView
    [self.playerWebView sendUrlStr:model.webview_url];
    NSLog(@"%@",model.webview_url);
    
    //调整动画
    [self.playerImageView StartAnimation];
    
    //传值给小player
    [self.playerViewDele backArray:self.listArray indexRow:self.indexRow firstBtn:1];
    
    //判断收藏状态
    [self likeStateJudge];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    self.pageC.currentPage = self.myScroll.contentOffset.x / (375 * EACH_WIDTH);
}

-(void)sliderAction{
    
    [self.player seekToTime:self.mySlider.value];
}

//一秒更新一次
- (void)timeAction{
    
    self.mySlider.maximumValue = self.player.duration;
    //progress当前时长
    self.mySlider.value = self.player.progress;
    
    //总分秒数
    NSInteger totalMin = (NSInteger)self.player.duration / 60;
    NSInteger totalSec = (NSInteger)self.player.duration % 60;
    
    NSInteger currentMin = (NSInteger)self.player.progress / 60;
    NSInteger currentSec = (NSInteger)self.player.progress % 60;
    
    self.startTime.text = [NSString stringWithFormat:@"%02ld:%02ld",currentMin, currentSec];
    self.totalTime.text = [NSString stringWithFormat:@"%02ld:%02ld",totalMin, totalSec];
    
    if (self.smallPlayerKey == 0) {
        self.btnJudge = 0;
    }
    
}


//上一首方法
-(void)lastAction{

    self.btnJudge = 1;
    self.outKey = 0;
    [self startAnimation];
    [self.startBtn setImage:[UIImage imageNamed:@"pause2.png"] forState:UIControlStateNormal];
    self.btnState = 1;
    if (self.indexRow == 0) {
        self.indexRow = self.listArray.count - 1;
        [self giveValue];
    }else{
            
        self.indexRow--;
        [self giveValue];
    }
}

//下一首方法
-(void)nextAction{
    
    self.btnJudge = 1;
    self.outKey = 0;
    self.btnState = 1;
    [self startAnimation];
    [self.startBtn setImage:[UIImage imageNamed:@"pause2.png"] forState:UIControlStateNormal];
    if (self.typeKey == 2) {
        
        self.indexRow = arc4random()%(self.listArray.count);
        [self giveValue];
    }else{
        
        if (self.indexRow == self.listArray.count - 1) {
            self.indexRow = 0;
            [self giveValue];
        }else{
            self.indexRow++;
            [self giveValue];
        }
    }
}

//播放/暂停方法
-(void)startAction{
    if (self.player.state == STKAudioPlayerStatePlaying) {
        [self.player pause];
        [self.startBtn setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
        self.btnState = 0;
        self.State = @"pause";
        [self stopAnimation];
        
    }else{
        [self.player resume];
        [self.startBtn setImage:[UIImage imageNamed:@"pause2.png"] forState:UIControlStateNormal];
        self.btnState = 1;
        self.State = @"play";
        [self startAnimation];
    }
    [self.playerViewDele backState:self.State];
}

-(void)stopAnimation{

    //每一个view系统的layer层系统都设置了一个记录时间的属性,通过改变view动画时间来控制动画效果
    //参数1.或得到当前旋转的时间点
    CFTimeInterval stopTime = [self.playerImageView.PicImage.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    //设置播放速度为0,停止
    self.playerImageView.PicImage.layer.speed = 0;
    //记录当前时间的偏移量
    self.playerImageView.PicImage.layer.timeOffset = stopTime;
}

-(void)startAnimation{

    //获得偏移量
    CFTimeInterval stopTime = self.playerImageView.PicImage.layer.timeOffset;
    //设置速度1.0
    self.playerImageView.PicImage.layer.speed = 1.0;
    //设置偏移量为0
    self.playerImageView.PicImage.layer.timeOffset = 0;
    //设置开始时间为0
    self.playerImageView.PicImage.layer.beginTime = 0;
    //设置开始时间
    self.playerImageView.PicImage.layer.beginTime = [self.playerImageView.PicImage.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
}

-(void)typeAction:(UIButton *)btn{

    self.typeKey++;
    if (self.typeKey == 0) {
        [self.typeBtn setImage:[UIImage imageNamed:@"xunhuan.png"] forState:UIControlStateNormal];
    }else if (self.typeKey == 1){
    
        [self.typeBtn setImage:[UIImage imageNamed:@"xunhuanOnce.png"] forState:UIControlStateNormal];
        self.animationView.backgroundColor = [UIColor colorWithRed:0.15 green:0.76 blue:0.48 alpha:1];
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            self.animationView.frame = CGRectMake(0, 0, 375 * EACH_WIDTH, 60 * EACH_HEIGHT);
            self.animationLabel.text = @"已切换到单曲循环模式";
        } completion:^(BOOL finished) {
            
            self.animationView.frame = CGRectMake(0, -60 * EACH_HEIGHT, 375 * EACH_WIDTH, 60 * EACH_HEIGHT);
        }];
    }else if (self.typeKey == 2){
    
        [self.typeBtn setImage:[UIImage imageNamed:@"suiji.png"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            self.animationView.frame = CGRectMake(0, 0, 375 * EACH_WIDTH, 60 * EACH_HEIGHT);
            self.animationLabel.text = @"已切换到随机播放模式";
        } completion:^(BOOL finished) {
            
            self.animationView.frame = CGRectMake(0, -60 * EACH_HEIGHT, 375 * EACH_WIDTH, 60 * EACH_HEIGHT);
        }];
    }else if (self.typeKey == 3){
    
        [self.typeBtn setImage:[UIImage imageNamed:@"xunhuan.png"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:1 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            self.animationView.frame = CGRectMake(0, 0, 375 * EACH_WIDTH, 60 * EACH_HEIGHT);
            self.animationLabel.text = @"已切换到顺序播放模式";
        } completion:^(BOOL finished) {
            
            self.animationView.frame = CGRectMake(0, -60 * EACH_HEIGHT, 375 * EACH_WIDTH, 60 * EACH_HEIGHT);
        }];
        self.typeKey = 0;
    }
    
}

-(void)backAction{

    if (self.btnState == 0) {
        [self.playerViewDele backState:@"pause"];
    }else{
        [self.playerViewDele backState:@"play"];
    }
//    [self.playerViewDele backState:self.State];
    
    [self dismissViewControllerAnimated:1 completion:^{
        
        
    }];
}

//playerListView协议方法
-(void)backIndexPath:(NSIndexPath *)indexPath{

    self.indexRow = indexPath.row;
    self.btnJudge = 1;
    self.outKey = 0;
    self.btnState = 1;
    [self.startBtn setImage:[UIImage imageNamed:@"pause2.png"] forState:UIControlStateNormal];
    [self startAnimation];
    [self giveValue];
}

//播放器协议方法
- (void) audioPlayer:(STKAudioPlayer *)audioPlayer didStartPlayingQueueItemId:(NSObject *)queueItemId{
    
    // NSLog(@"开始播放");
}

- (void) audioPlayer:(STKAudioPlayer *)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject *)queueItemId{
    
    //  NSLog(@"完成加载");
}

- (void) audioPlayer:(STKAudioPlayer *)audioPlayer stateChanged:(STKAudioPlayerState)state previousState:(STKAudioPlayerState)previousState{
    
    //  NSLog(@"播放状态改变");
}


- (void)audioPlayer:(STKAudioPlayer *)audioPlayer logInfo:(NSString *)line{
    
}

- (void) audioPlayer:(STKAudioPlayer *)audioPlayer unexpectedError:(STKAudioPlayerErrorCode)errorCode{
    
}

- (void) audioPlayer:(STKAudioPlayer *)audioPlayer didCancelQueuedItems:(NSArray *)queuedItems{
    
}

-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishPlayingQueueItemId:(NSObject*)queueItemId withReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration{
    
    
    if (self.typeKey == 1) {
        //单曲循环
        if (self.btnJudge == 1) {
            self.btnJudge = 0;
        }else{
            
            [self giveValue];
        }

    }else if(self.typeKey == 2){
        //随机循环
        if (self.btnJudge == 1) {
        
        }else{
            
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                self.tempRow = self.indexRow ;//共产党只走一次!!!!!
                
            });
            self.tempRow = arc4random()%(self.listArray.count);
            self.indexRow = self.tempRow;
            [self giveValue];
        }
        
    }else{
        NSLog(@"%d",self.btnJudge);
        //顺序循环
        if (self.btnJudge == 1) {

        }else{
            
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                self.tempRow = self.indexRow ;//共产党只走一次!!!!!
                
            });
            if (self.tempRow == self.listArray.count - 1) {
                
                self.tempRow = 0;
            }
            self.tempRow ++;
            self.indexRow = self.tempRow;
            [self giveValue];
        }
    }
    
}

//小播放器协议方法
-(void)getSmallPlayerData:(NSInteger)indexRow{

    self.indexRow = indexRow;
    self.outKey = 0;
    [self.startBtn setImage:[UIImage imageNamed:@"pause2.png"] forState:UIControlStateNormal];
    NSLog(@"-----%ld",(long)self.indexRow);
    self.btnJudge = 1;
    [self giveValue];
    
}

-(void)likeAction:(UIButton *)btn{

    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.bgImage addSubview:self.hud];
    HJJRadioRootModel *model = [self.listArray objectAtIndex:self.indexRow];
    if (self.likeState == 0) {
        [self.likeBtn setImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];
        //插入数据
        Radio *radio = [NSEntityDescription insertNewObjectForEntityForName:@"Radio" inManagedObjectContext:[[CoreDataManager shareManager] managedObjectContext]];
        radio.musicUrl = model.musicUrl;
        radio.coverimg = model.coverimg;
        radio.webview_url = model.webview_url;
        radio.uname = model.uname;
        radio.title = model.title;
        //保存
        [[CoreDataManager shareManager] saveContext];
        NSLog(@"插入成功");
        
        self.hud.color = [UIColor colorWithRed:0.22 green:0.69 blue:0.76 alpha:1];
        self.hud.labelText = @"收藏成功";
        self.hud.dimBackground = NO;
        [self.hud show:YES];
        self.hud.labelFont = [UIFont systemFontOfSize:16];
        self.hud.mode = MBProgressHUDModeCustomView;
        self.hud.backgroundColor = [UIColor clearColor];
        [self.hud hide:YES afterDelay:1.0];
    }else{
    
        [self.likeBtn setImage:[UIImage imageNamed:@"disLike.png"] forState:UIControlStateNormal];
        [CoreData deleteData:model.musicUrl];
        
        self.hud.color = [UIColor colorWithRed:0.73 green:0.8 blue:0.87 alpha:1];
        self.hud.labelText = @"取消收藏";
        self.hud.dimBackground = NO;
        [self.hud show:YES];
        self.hud.labelFont = [UIFont systemFontOfSize:16];
        self.hud.mode = MBProgressHUDModeCustomView;
        self.hud.backgroundColor = [UIColor clearColor];
        [self.hud hide:YES afterDelay:1.0];
    }
    self.likeState = !self.likeState;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
